package com.jr.controller;

import com.jr.pojo.LUser;
import com.jr.pojo.PersonResume;
import com.jr.service.impl.UserServiceImpl;
import com.jr.uitil.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl usi;



    @RequestMapping("login")
    @ResponseBody
    public LUser login(String input,String password,Integer role){
        //登录，可以使用util包中的加密类进行判断
        //判断当前输入的是用户名还是邮箱
        System.out.println("进入user/login");
        LUser u = new LUser();
        u.setUsertype(role);
        System.out.println(input+"input*********");
        System.out.println(password+"pwd*********");
        if(input.contains("@")){
            u.setEmail(input);
        }else{
            u.setUsername(input);
        }

        //后面需要调用加密方法判断输入的密码是否符合数据库中的加密密码
        //可以这么调用 Encryption.encrypsw(password),获得一个加密的字符串，再赋给这个局部变量用户
        u.setPassword(Encryption.encrypsw(password));
        System.out.println();

        System.out.println("u："+u);
        return usi.login(u);
    }

    @RequestMapping("register")
    @ResponseBody
    public boolean register(String username,String email,String password){
        String psw = Encryption.encrypsw(password);
        System.out.println(psw);
        //新建的用户都为普通用户，默认设置为0
        System.out.println("用户名为"+username);
        System.out.println("邮箱为:"+email);
        LUser u = new LUser(null,username,email,psw,0,null);
        boolean b = false;
        try{
           b= usi.register(u);
            System.out.println("是否注册成功:"+b);
            return b;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("forgetpsw")
    @ResponseBody
    public boolean resetpsw(String username,String email,String password){

        String psw = Encryption.encrypsw(password);
        System.out.println(psw);
        LUser u = new LUser(null,username,email,psw,0,null);
        boolean b =usi.updatePsw(u);
        System.out.println(b);
        return b;
    }


    @RequestMapping("updateprofile")
    @ResponseBody
    public boolean updateprofile(String username,String name,String email,String phone,String bio){
        System.out.println("进入用户信息修改的方法");
        PersonResume pr = new PersonResume(null,username,name,email,phone,bio);
        System.out.println("传入的信息pr："+pr);
        boolean b = usi.updatePersonInfo(pr);
        System.out.println(b);
        return b;
    }

    @RequestMapping("checkvip")
    @ResponseBody
    public int checkvip(Integer userid){
        System.out.println("过来拿vip了吗");
        System.out.println(userid);
        return usi.queryVip(userid);
    }
}
